/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entities;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="CARTS")
@NamedQueries({
    @NamedQuery(name = "getAllCarts",
    query = "SELECT c FROM Cart c"),
    @NamedQuery(name = "removeCartForId",
    query = "DELETE FROM Cart a WHERE a.id = :cartID")
})
public class Cart implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID", nullable = false)
    private Long id;
    
    @OneToOne(mappedBy="cart")
    private Client clientId;
    
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "CARTS_BOOKS", joinColumns = {
        @JoinColumn(name = "CART_ID")
    }, inverseJoinColumns = {
        @JoinColumn(name = "BOOK_ID")
    })
    private Collection<Book> books;

    public Cart(Client clientId) {
        this.clientId = clientId;
        books = new ArrayList<Book>();
    }

    public Cart() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Collection<Book> getBooks() {
        return books;
    }

    public void setBooks(Collection<Book> books) {
        this.books = books;
    }

    public void setClientId(Client clientId) {
        this.clientId = clientId;
    }

    public Client getClientId() {
        return clientId;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Cart)) {
            return false;
        }
        Cart other = (Cart) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Order[id=" + id + "]";
    }
    
    public double getCurrentValue()
    {
        double totalValue=0;
        for(Book book: books)
            totalValue += book.getPrice();

        BigDecimal bd = new BigDecimal(totalValue);
        bd = bd.setScale(2,BigDecimal.ROUND_UP);
        //bd
        return bd.doubleValue();
    }
    public void addBook(Book book)
    {
        books.add(book);
    }

    public void removeBook(Book book) {
        books.remove(book);
    }
}
